using System;
using System.Collections.Generic;
using System.Diagnostics;
using Common.Logging;
using NServiceBus.Management.Messages;
using NServiceBus.Management.Messages.Status;

namespace NServiceBus.Management.Managers.Impl
{
    public class StatusManager : ManagerBase,
                                 IStatusManager
  {

        private static readonly ILog logger = LogManager.GetLogger(typeof(StatusManager));

 
        public void SendEndpointStatus()
        {
            try
            {
                var status = new StatusMessage
                                 {
                                     CreatedAt = DateTime.Now,
                                     EndpointName = Process.GetCurrentProcess().ProcessName,
                                     EndpointId = Process.GetCurrentProcess().Id + Environment.MachineName,
                                     HostName = Environment.MachineName,
                                     NumberOfWorkerThreads = Transport.NumberOfWorkerThreads,
                                     MessageTypes = (List<Type>) Bus.RegisteredMessageTypes,
                                     InputAdress = Transport.Address
                                 };


                Bus.Send(status);
               
                logger.Debug("StatusMessage sent to management endpoint");

            }
            catch (Exception ex)
            {
                logger.Fatal("Failed to send StatusMessage", ex);
            }
        }
    }
}
